package org.jeecgframework.web.system.controller.core;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Property;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.UploadFile;
import org.jeecgframework.core.common.model.json.*;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.enums.SysThemesEnum;
import org.jeecgframework.core.util.*;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.DataTableReturn;
import org.jeecgframework.tag.vo.datatable.DataTables;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.tag.vo.easyui.TreeGridModel;
import org.jeecgframework.web.system.manager.ClientManager;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


/**
 * @ClassName: SystemSettingsController
 * @Description: TODO(参数管理处理类)
 * @author 张代浩
 */
//@Scope("prototype")
@Controller
@RequestMapping("/systemSettingsController")
public class SystemSettingsController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SystemSettingsController.class);

	private UserService userService;
	private SystemService systemService;

	@Autowired
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 系统参数列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView user(HttpServletRequest request) {
			return new ModelAndView("system/settings/systemSettingsList");
	}

	/**
	 * easyuiAJAX用户列表请求数据 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(TSSettings settings, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
//        String settingName = request.getParameter("settingName");
        CriteriaQuery cq = new CriteriaQuery(TSSettings.class, dataGrid);
        // 查询条件组装器
        if(!StringUtils.isBlank(settings.getSettingName())){
						String settingName = "%" + settings.getSettingName() + "%";
            cq.like("settingName", settingName);
            cq.add();
        }
        cq.addOrder("settingName", SortDirection.asc);
//        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, settings, request.getParameterMap());
        this.systemService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

	/**
	 * easyuiAJAX请求数据： 用户选择角色列表
	 *
	 * @param req
	 * @param settings
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(TSSettings settings, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(settings.getSettingName())) {
			settings = systemService.getEntity(TSSettings.class, settings.getSettingName());
			req.setAttribute("settings", settings);
		}
		return new ModelAndView("system/settings/systemSettings");
	}

	/**
	 * 保存参数设置
	 *
	 * @param settings
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(TSSettings settings, HttpServletRequest request) {
		String message = null;
		AjaxJson json = new AjaxJson();
		if (StringUtil.isNotEmpty(settings.getSettingName())) {
			message = "参数: " + settings.getSettingName() + "被更新成功";
			systemService.saveOrUpdate(settings);
		}
		json.setMsg(message);
		return json;
	}
}